<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AngularJS&middot;Hello AngularJS</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.css">
    <style>
        .demo{
            margin-bottom:10px;
            border-bottom:1px solid #1b926c;
        }
    </style>
</head>
<body>

 <div class="container">
     <div class="row">
         <nav class="navbar navbar-default navbar-static-top">
             <div class="container-fluid">
                 <div class="navbar-header">
                     <a class="navbar-brand" href="../index.html">首页</a>
                 </div>
                 <ul class="nav navbar-nav">
                    <li class="active"><a href="javascript:void(0)">Link</a></li>
                    <li><a href="javascript:void(0)">Link2</a></li>
                 </ul>
             </div>
         </nav>
     </div>
     <div class="row demo" ng-app="MyAPP">
         <h3>angularjs指令$interpolate,类似Handlerbars的模版渲染指令示例</h3>
         <div class="col-md-12" ng-controller="MyCtrl">
             {{headerhtml}}
             <div class="col-sm-6">
                 {{str1}}
             </div>
             <div class="col-sm-6">
                 {{str2}}
             </div>
         </div>
         <div class="col-md-12" ng-controller="SecondCtrl">
                 <input ng-model="myName" type="text" placeholder="Type Your Name">
                 <textarea ng-model="myTextarea" cols="30" rows="10"></textarea>
                 <p>结果：</p>
                 <div ng-bind="interpolatedValue"></div>
         </div>
     </div>

 </div>
 </div>
</body>
</html>
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular.js" type="text/javascript"></script>
<script>

    var app=angular.module("MyAPP",[]);
    app.controller("MyCtrl",function($scope,$interpolate){
        var tmp = $interpolate('Publish by {{name}} on {{date}}');
        var data1 = {name: 'Mike',date: '20160226'};
        var data2 = {name: 'Karen',date: '20160226'};

        $scope.str1=tmp(data1);
        $scope.str2=tmp(data2);

        //TODO 深挖$interpolate，看下面的代码为啥不行？？？
        //var logotpl=$interpolate('<img src="{{logoURL}}"/>');
        //var logodata={logoURL:"http://cdn.angularjs.cn/img/logo.png"};
        //$scope.headerhtml=logotpl(logodata);
    });
    app.controller("SecondCtrl",function($scope,$interpolate){
        $scope.$watch("myTextarea", function(newVal, oldVal, scope){
            var interpolatedFunc = $interpolate(newVal);
            $scope.interpolatedValue = interpolatedFunc({myName: $scope.myName});
            //$scope.interpolatedValue = interpolatedFunc(scope);
        });
    });
</script>
